// miniprogram/pages/order/orderInfo.js
import moment from "../../utils/moment.js";
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        order: {},
        status: {
            1: '完成',
            2: '待付款',
            3: '代发货',
            4: '待收货'
        }
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        if (options.orderId) {
            this.getOrder(options.orderId);
        }
    },
    getOrder(orderId) {
        const db = wx.cloud.database()
        //获取商品列表
        db.collection('order_list').doc(orderId).get({
            success: res => {
                let data = this.formatData(res.data)
                this.setData({
                    order: data
                })
                console.log(this.data.order)
            },
            fail: err => {
                wx.showToast({
                    icon: 'none',
                    title: '查询记录失败'
                })
                console.error('[数据库] [查询记录] 失败：', err)
            }
        })
    },
    formatData(data) {
        let tmpStatus = data.status;
        data.status = {};
        data.status.style = "status-" + tmpStatus;
        data.status.value = tmpStatus;
        data.status.name = this.data.status[tmpStatus];
        data.complete_time = moment(data.complete_time).format("YYYY-MM-DD HH:mm:ss")
        data.consignee_time = moment(data.consignee_time).format("YYYY-MM-DD HH:mm:ss")
        data.order_time = moment(data.order_time).format("YYYY-MM-DD HH:mm:ss")
        data.pay_time = moment(data.pay_time).format("YYYY-MM-DD HH:mm:ss")
        data.totalPrice = 0;
        data.goods_list.forEach(item => {
            data.totalPrice += parseFloat(item.parameter.price);
        })
        return data
    },
    open: function() {
        let _this = this;
        wx.showActionSheet({
            itemList: ['已收货'],
            success: function(res) {
                if (!res.cancel) {
                    const db = wx.cloud.database()
                    db.collection('order_list').doc(_this.data.order._id).update({
                        data: {
                            status: 1
                        },
                        success: res => {
                            console.log(res)
                            wx.redirectTo({
                                url: `/pages/order/orderInfo?orderId=${_this.data.order._id}`,
                            })
                        },
                        fail: err => {
                            wx.showToast({
                                icon: 'none',
                                title: '查询记录失败'
                            })
                            console.error('[数据库] [查询记录] 失败：', err)
                        }
                    })
                }
            }
        });
    },
})